﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using XmlStudio.Logger.Interfaces;

namespace XmlStudio.Logger
{
    /// <summary>
    /// Static Log class.
    /// </summary>
    public static class Log
    {
        #region Fields
        private static ILog log;
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the reference to the current log instance.
        /// </summary>
        /// <exception cref="System.ApplicationException">When log was not started.</exception>
        public static ILog Current
        {
            get
            {
                if (log == null)
                {
                    throw new ApplicationException("Log not started.");
                }
                else
                {
                    Debug.Assert(log != null);

                    return log;
                }
            }

            set
            {
                log = value;
            }
        }

        /// <summary>
        /// Gets the reference to the current log writer.
        /// </summary>
        /// <exception cref="System.ApplicationException">When the log was not started.</exception>
        public static ILogWriter CurrentLogWriter
        {
            get
            {
                if (log == null)
                {
                    throw new ApplicationException("Log not started.");
                }
                else
                {
                    Debug.Assert(log != null);

                    return log.CurrentLogWriter;
                }
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Starts the log.
        /// </summary>
        /// <param name="path">Path to the log file to write to.</param>
        /// <returns>Reference to the log instance.</returns>
        /// <exception cref="System.ArgumentException">When path is null or empty.</exception>
        /// <exception cref="System.ApplicationException">When log has already been started.</exception>
        public static ILog Start(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("path is null or empty.");
            }

            if (log != null)
            {
                throw new ApplicationException("log has alredy been started.");
            }

            log = new LogImplementor();
            Debug.Assert(log != null);
            return log.Start(path);
        }

        /// <summary>
        /// Closes and stops the log.
        /// </summary>
        public static void DisposeLog()
        {
            if (log != null)
            {
                log.DisposeLog();
                log = null;
            }
        }
        #endregion
    }
}